package com.shop.dao.imp;

import com.shop.beans.Address;
import com.shop.beans.Goodstype;
import com.shop.beans.User;
import com.shop.dao.GoodstypeDao;
import com.shop.dao.UserDao;
import com.shop.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class GoodstypeDaoImp01 implements GoodstypeDao {
    @Override
    public void addGoodstype(Goodstype goodstype) {
        try {
            Connection connection=new JDBCUtil().getConnection();
            String sql="insert into t_goodstype values(null,?,?,?)";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,goodstype.getGoodstype());
            ps.setString(2,goodstype.getMark());
            ps.setInt(3,goodstype.getParent_id());
            ps.execute();
            System.out.println("【INFO】=>ADD-[ACC]添加商品类型成功名为"+goodstype.getGoodstype()+"\n\t\t->|"+goodstype);
            connection.close();
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateGoodstype(Goodstype goodstype) {
        try {
            int id=goodstype.getId();
            GoodstypeDao goodstypeDao = new GoodstypeDaoImp01();

            if(goodstypeDao.getGoodstypeById(id)==null){
                System.out.println("【INFO】=>UP-[FAL]修改类型失败，无法找到id为"+
                        goodstype.getId()+"的类型");
                return;
            }
            Connection connection= JDBCUtil.getConnection();
            String sql="update t_goodstype set  goodstype=? , mark=? , parent_id=? where id=?";
            PreparedStatement ps= connection.prepareStatement(sql);
            ps.setString(1,goodstype.getGoodstype());
            ps.setString(2,goodstype.getMark());
            ps.setInt(3,goodstype.getParent_id());
            ps.setInt(4,id);
            ps.execute();
            System.out.println("【INFO】=>UP-[ACC]修改id为"+id+"用户id为"+goodstype.getId()+"的地址成功\n\t\t->|"+goodstype);
            connection.close();

        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteGoodstype(int id) {
        try{
            Connection connection= JDBCUtil.getConnection();
            String sql="delete  from t_goodstype where id=?";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setInt(1,id);
            ps.execute();
            System.out.println("【INFO】=>DEL-[ACC]删除id为"+id+"类型为"+"的类型成功\n\t\t->|");
            connection.close();
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public Goodstype getGoodstypeById(int id) {
        Goodstype goodstype1=new Goodstype();
        try{
            Connection connection=JDBCUtil.getConnection();
            String sql="select * from t_goodstype where id=?";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setInt(1,id);
            ResultSet resultSet=ps.executeQuery();
            while (resultSet.next()){
                String goodstype=resultSet.getString("goodstype");
                String mark=resultSet.getString("mark");
                int parent_id=resultSet.getInt("parent_id");
                Goodstype goodstype2=new Goodstype(id,goodstype,mark,parent_id);
                goodstype1=goodstype2;
                connection.close();
                break;
            }
            if ((goodstype1.getGoodstype()==null)&&(goodstype1.getMark()==null)){
                System.out.println("【INFO】=>Getgoodstype-[FAL]获取类型id为"+id+"失败");
                return null;
            }
            System.out.println("【INFO】=>Getgoodstype-[ACC]获取类型成功\n\t\t->|"+goodstype1);
            return goodstype1;
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Goodstype> getAllGoodstype() {
        List<Goodstype> goodstypeList=new ArrayList<>();
        try{
            Connection connection= JDBCUtil.getConnection();
            String sql="select * from t_goodstype ";
            PreparedStatement ps=connection.prepareStatement(sql);
            ResultSet resultSet= ps.executeQuery();
            //ResultSet resultSet= executeQuery(sql);
            System.out.println("【INFO】=>GetAddresss-[ACC]获取类型成功用户id为");
            while (resultSet.next()){
                int id=resultSet.getInt("id");
                String goodstype=resultSet.getString("goodstype");
                String mark=resultSet.getString("mark");
                int parent_id=resultSet.getInt("parent_id");
                Goodstype goodstype1=new Goodstype(id,goodstype,mark,parent_id);
                     System.out.println("\t\t->|"+goodstype1);
                goodstypeList.add(goodstype1);
            }
            System.out.println("\t\t->|共计"+goodstypeList.size());
            connection.close();
            return goodstypeList;
            //ps.execute();
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Goodstype> getGoodstypeByParent(int parentid) {
        List<Goodstype> goodstypeList=new ArrayList<>();
        try{
            Connection connection= JDBCUtil.getConnection();
            String sql="select * from t_goodstype where parent_id=? ";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setInt(1,parentid);
            ResultSet resultSet= ps.executeQuery();
            //ResultSet resultSet= executeQuery(sql);
            System.out.println("【INFO】=>Get类型-[ACC]获取类型成功，父id为"+parentid);
            while (resultSet.next()){
                int id=resultSet.getInt("id");
                String goodstype=resultSet.getString("goodstype");
                String mark=resultSet.getString("mark");
                int parent_id=resultSet.getInt("parent_id");
                Goodstype goodstype1=new Goodstype(id,goodstype,mark,parent_id);
                System.out.println("\t\t->|"+goodstype1);
                goodstypeList.add(goodstype1);
            }
            System.out.println("\t\t->|共计"+goodstypeList.size());
            connection.close();
            return goodstypeList;
            //ps.execute();
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }
}
